/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.TipoTarifa;

/**
 *
 * @author roderick
 */


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

public class TipoTarifaModel  extends DataBase{
    
    private int id;
    private String ttadesc;
    private String ttaabr;
    private boolean ttapred;

    //ttacod,ttadesc,ttaabr,ttapred

    public TipoTarifaModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO tipotarifas(ttadesc, ttaabr, ttapred)VALUES (?, ?, ?);");
                stmt.setString(1,this.ttadesc);
                stmt.setString(2,this.ttaabr);
                stmt.setBoolean(3, this.ttapred);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE tipotarifas SET ttadesc=?, ttaabr=?, ttapred=? where ttacod = ? ;");
                stmt.setString(1,this.ttadesc);
                stmt.setString(2,this.ttaabr);
                stmt.setBoolean(3, this.ttapred);
                stmt.setInt(4, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from tipotarifas where ttacod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT ttacod, ttadesc, ttaabr, CASE WHEN ttapred=False THEN 'No' WHEN ttapred=True THEN 'Si' END as ttapred FROM tipotarifas;");
    }


    public ResultSet finder(String var){
        return this.find("SELECT ttacod, ttadesc, ttaabr, CASE WHEN ttapred=False THEN 'No' WHEN ttapred=True THEN 'Si' END as ttapred FROM tipotarifas WHERE ttadesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT ttacod, ttadesc, ttaabr,ttapred "+
                         "FROM tipotarifas WHERE ttacod = "+var+";");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTtaabr() {
        return ttaabr;
    }

    public void setTtaabr(String ttaabr) {
        this.ttaabr = ttaabr;
    }

    public String getTtadesc() {
        return ttadesc;
    }

    public void setTtadesc(String ttadesc) {
        this.ttadesc = ttadesc;
    }

    public boolean isTtapred() {
        return ttapred;
    }

    public void setTtapred(boolean ttapred) {
        this.ttapred = ttapred;
    }

}
